/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann Tübingen, Germany 
 * Niko Schenk Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package collectors.impl;

import collectors.api.Collector1;
import util.Interval;
import util.LinkedInterval;
import saphre.core.Store;
import saphre.core.SuffixArray;
import util.sorting.Multiset;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
import collectors.impl.visitors.CollectTraversal;

/**
 * Class which simulates lcp-interval tree traversal by implementing the
 * Collector interface.
 *
 * @author Dale Gerdemann, Niko Schenk
 */
public class CollectLevelIntervals implements Collector1 {

    Store store;
    SuffixArray sa;
    double D;
    Interval top;
    int n = 0;
    int[] psi;
    List<List<Interval>> lList = new ArrayList<List<Interval>>();

    public List<List<Interval>> getlList() {
        return lList;
    }
    List<Set<LinkedInterval>> nList = new ArrayList<Set<LinkedInterval>>();

    public List<Set<LinkedInterval>> getnList() {
        return nList;
    }

    public CollectLevelIntervals(Store store, SuffixArray sa, Interval top) {
        this.store = store;
        this.sa = sa;
        this.top = top;
        CollectTraversal.pseudoDepth = 0;
        psi = sa.psi();
    }

    public void addPrefixArray(SuffixArray pa) {
    }

    @Override
    public void preAdd(Interval inter, int parentLcp, int lcp, int depth) {
    }

    @Override
    public void add(Interval inter, int lcp, int depth, Multiset docDist,
            Multiset leftContext) {
        if (lcp > 0) {
            while (lList.size() <= lcp + 1) {
                lList.add(new ArrayList<Interval>());
                nList.add(new TreeSet<LinkedInterval>());
            }
            lList.get(lcp).add(inter);
            if (lcp > 1) {
                Interval psiInter =
                        new Interval(psi[sa.getSuftab()[inter.lb()] + 1],
                        psi[sa.getSuftab()[inter.rb()] + 1]);
                //System.out.println(inter+" "+psiInter);
                LinkedInterval linkedInter =
                        new LinkedInterval(psiInter, inter.mid(sa.getChildTable()));
                nList.get(lcp).add(linkedInter);
            }
        }
    }

    @Override
    public void addTrivial(Interval inter, int lcp, int depth,
            Multiset docDist, Multiset leftContext) {
    }
}
